package com.mr.wordcount;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class STJoinMain extends Configured implements Tool {


    public static class Map extends Mapper<Object, Text, Text, Text> {


        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] lines = line.split(" ");
            String child = lines[0];
            String parent = lines[1];

            System.out.println(child+" "+parent);

            context.write(new Text(child), new Text("==" + parent));
            context.write(new Text(parent), new Text("--" + child));

        }
    }

    public static class Reduce extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> childs=new ArrayList<String>();
            List<String> parents=new ArrayList<String>();


            for (Text value : values) {
                System.out.println(key+" "+value);
                String v=value.toString();
                if(v.startsWith("==")){
                    parents.add(v);
                }else {
                    childs.add(v);
                }
            }

            for(String child:childs){
                for (String parent:parents){
                    System.out.println(child+" "+parent);
                    context.write(new Text(child),new Text(parent));
                }
            }

        }
    }


    public int run(String[] args) throws Exception {
        Job job = new Job(getConf());
        job.setJarByClass(STJoinMain.class);
        job.setJobName("StJoinMain");


        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);


        job.setMapperClass(STJoinMain.Map.class);
        job.setReducerClass(STJoinMain.Reduce.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);


        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean success = job.waitForCompletion(true);
        return success ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new STJoinMain(), args);
        System.exit(res);
    }
}
